package createThread;

import java.util.Stack;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @program: javase
 * @description: 实现Callable接口
 * @author: lld
 * @create: 2021-03-13 18:35
 **/
class myThread3 implements Callable<String> {

    //泛型对应call方法的返回值
    @Override
    public String call() throws Exception {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
//                Thread.currentThread().setName("createThread.MyThread");
                System.out.println(i + Thread.currentThread().getName());
            }
        }
        return Thread.currentThread().getName();
    }
}

public class Thread3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
////        异步任务
        FutureTask<String> futureTask = new FutureTask<>(new myThread3());
//        FutureTask实现了RunnableFuture接口，RunnableFuture接口继承自Runnable接口
//        通过get方法获取返回值
        Thread thread = new Thread(futureTask, "createThread.myThread3");
        thread.start();
        //获取自定义线程的返回值，get方法会造成线程阻塞，要线程执行完才返回
        System.out.println(futureTask.get());
////        main主线程
        for (int i = 0; i < 100; i++) {
            if (i % 3 == 0) {
                System.out.println(i + Thread.currentThread().getName());
            }

        }
//        System.out.println(futureTask.get());

    }
}
